package org.nstack.header.storage.backup;

import org.nstack.header.query.*;
import org.nstack.header.storage.snapshot.VolumeSnapshotBackupStorageRefInventory;
import org.nstack.header.configuration.PythonClassInventory;
import org.nstack.header.image.ImageBackupStorageRefInventory;
import org. nstack.header.query.*;
import org.nstack.header.search.Inventory;
import org.nstack.header.search.TypeField;

import javax.persistence.JoinColumn;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Inventory(mappingVOClass = BackupStorageVO.class)
@PythonClassInventory
@ExpandedQueries({
        @ExpandedQuery(expandedField = "zoneRef", inventoryClass = BackupStorageZoneRefInventory.class,
                foreignKey = "uuid", expandedInventoryKey = "backupStorageUuid", hidden = true),
        @ExpandedQuery(expandedField = "imageRef", inventoryClass = ImageBackupStorageRefInventory.class,
                foreignKey = "uuid", expandedInventoryKey = "backupStorageUuid", hidden = true),
        @ExpandedQuery(expandedField = "volumeSnapshotRef", inventoryClass = VolumeSnapshotBackupStorageRefInventory.class,
                foreignKey = "uuid", expandedInventoryKey = "backupStorageUuid", hidden = true),
})
@ExpandedQueryAliases({
        @ExpandedQueryAlias(alias = "zone", expandedField = "zoneRef.zone"),
        @ExpandedQueryAlias(alias = "image", expandedField = "imageRef.image"),
        @ExpandedQueryAlias(alias = "volumeSnapshot", expandedField = "volumeSnapshotRef.volumeSnapshot"),
})
public class BackupStorageInventory implements Serializable {

    private String uuid;

    private String name;

    private String url;

    private String description;

    private Long totalCapacity;

    private Long availableCapacity;

    @TypeField
    private String type;

    private String state;

	private String status;



    private Timestamp createDate;

    private Timestamp lastOpDate;

    @Queryable(mappingClass = BackupStorageZoneRefInventory.class,
            joinColumn = @JoinColumn(name = "backupStorageUuid", referencedColumnName = "zoneUuid"))
    private List<String> attachedZoneUuids;

    protected BackupStorageInventory(BackupStorageVO vo) {
        this.setCreateDate(vo.getCreateDate());
        this.setDescription(vo.getDescription());
        this.setLastOpDate(vo.getLastOpDate());
        this.setName(vo.getName());
        this.setState(vo.getState().toString());
        this.setStatus(vo.getStatus().toString());
        this.setTotalCapacity(vo.getTotalCapacity());
        this.setAvailableCapacity(vo.getAvailableCapacity());
        this.setType(vo.getType());
        this.setUrl(vo.getUrl());
        this.setUuid(vo.getUuid());
        this.attachedZoneUuids = new ArrayList<String>(vo.getAttachedZoneRefs().size());
        for (BackupStorageZoneRefVO ref : vo.getAttachedZoneRefs()) {
            if (!this.attachedZoneUuids.contains(ref.getZoneUuid())) {
                this.attachedZoneUuids.add(ref.getZoneUuid());
            }
        }
    }

    public BackupStorageInventory() {
    }

    public static BackupStorageInventory valueOf(BackupStorageVO vo) {
        BackupStorageInventory inv = new BackupStorageInventory(vo);
        return inv;
    }

    public static List<BackupStorageInventory> valueOf(Collection<BackupStorageVO> vos) {
        List<BackupStorageInventory> invs = new ArrayList<BackupStorageInventory>(vos.size());
        for (BackupStorageVO vo : vos) {
            invs.add(BackupStorageInventory.valueOf(vo));
        }
        return invs;
    }

    public long getAvailableCapacity() {
        return availableCapacity;
    }

    public void setAvailableCapacity(long availableCapacity) {
        this.availableCapacity = availableCapacity;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getTotalCapacity() {
        return totalCapacity;
    }

    public void setTotalCapacity(long totalCapacity) {
        this.totalCapacity = totalCapacity;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public Timestamp getLastOpDate() {
        return lastOpDate;
    }

    public void setLastOpDate(Timestamp lastOpDate) {
        this.lastOpDate = lastOpDate;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public List<String> getAttachedZoneUuids() {
        return attachedZoneUuids;
    }

    public void setAttachedZoneUuids(List<String> attachedZoneUuids) {
        this.attachedZoneUuids = attachedZoneUuids;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}
